///////////////////////////////////////////////////////////////////////////////
//
// MyDetours.cpp
//
// Sample PokerStars/FullTilt online poker botting code. For more information:
//
// http://www.codingthewheel.com/archives/how-i-built-a-working-online-poker-bot-5
//
// DISCLAIMER OF WARRANTY 
//
// THIS SOURCE CODE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES AS TO PERFORMANCE
// OR MERCHANTABILITY. THE AUTHOR AND/OR DISTRIBUTORS OF THIS SOURCE CODE MAY
// HAVE MADE STATEMENTS ABOUT THIS SOURCE CODE. ANY SUCH STATEMENTS DO NOT 
// CONSTITUTE WARRANTIES AND SHALL NOT BE RELIED ON BY THE USER IN DECIDING 
// WHETHER TO USE THIS SOURCE CODE.
//
// THIS SOURCE CODE IS PROVIDED WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES
// WHATSOEVER. BECAUSE OF THE DIVERSITY OF CONDITIONS AND HARDWARE UNDER 
// WHICH THIS SOURCE CODE MAY BE USED, NO WARRANTY OF FITNESS FOR A PARTICULAR
// PURPOSE IS OFFERED. THE USER IS ADVISED TO TEST THE SOURCE CODE THOROUGHLY
// BEFORE RELYING ON IT. THE USER MUST ASSUME THE ENTIRE RISK OF USING THE
// SOURCE CODE.
//
///////////////////////////////////////////////////////////////////////////////



#include "stdafx.h"
#include "Watch.h"
#include "MyDetours.h"
#include "WatchApp.h"
#include "Utils.h"






///////////////////////////////////////////////////////////////////////////////
// This block contains definitions for function pointers:
//
// "Real_TextOut IS A pointer-to-a-function. The function pointed to takes
// an HDC, two ints, an LPCWSTR, and another int, and it returns a BOOL."
//
// If we want to call the original (un-detoured) versions of the Windows
// functions we've detoured, we have to do so through these function pointers,
// using regular function-call syntax:
//
//     BOOL bReturnValue = Real_TextOut(a0, a1, a2, a3, a4);
//
// And so forth.
///////////////////////////////////////////////////////////////////////////////
BOOL (WINAPI * Real_TextOut)(HDC a0, int a1, int a2, LPCWSTR a3, int a4) = TextOutW;
BOOL (WINAPI * Real_ExtTextOut)(HDC a0, int a1, int a2, UINT a3, CONST RECT* a4, LPCWSTR a5, UINT a6, CONST INT* a7) = ExtTextOutW;
int (WINAPI * Real_DrawText)(HDC a0, LPCWSTR a1, int a2, LPRECT a3, UINT a4) = DrawTextW;
int (WINAPI * Real_DrawTextEx)(HDC a0, LPWSTR a1, int a2, LPRECT a3, UINT a4, LPDRAWTEXTPARAMS a5) = DrawTextExW;
HFILE (WINAPI * Real_OpenFile)(LPCSTR, LPOFSTRUCT, UINT) = OpenFile;
HANDLE (WINAPI * Real_CreateFile)(LPCWSTR lpFileName,DWORD dwDesiredAccess,DWORD dwShareMode,LPSECURITY_ATTRIBUTES lpSecurityAttributes,DWORD dwCreationDisposition,DWORD dwFlagsAndAttributes,HANDLE hTemplateFile) = CreateFileW;
BOOL (WINAPI * Real_WriteFile)(HANDLE hFile,LPCVOID lpBuffer,DWORD nNumberOfBytesToWrite,LPDWORD lpNumberOfBytesWritten,LPOVERLAPPED lpOverlapped) = WriteFile;
BOOL (WINAPI * Real_CreateProcess)(LPCWSTR lpApplicationName,LPWSTR lpCommandLine,LPSECURITY_ATTRIBUTES lpProcessAttributes,LPSECURITY_ATTRIBUTES lpThreadAttributes,BOOL bInheritHandles,DWORD dwCreationFlags,LPVOID lpEnvironment,LPCWSTR lpCurrentDirectory,LPSTARTUPINFOW lpStartupInfo,LPPROCESS_INFORMATION lpProcessInformation) = CreateProcessW;
HANDLE (WINAPI * Real_OpenProcess)(DWORD dwDesiredAccess,BOOL bInheritHandle,DWORD dwProcessId) = OpenProcess;



///////////////////////////////////////////////////////////////////////////////
// Our generic text-handling routine for Detoured GDI functions such as
// TextOut and ExtTextOut.
///////////////////////////////////////////////////////////////////////////////
void HandleGdiText(HDC hdc, LPCWSTR text, int X, int Y, LPCWSTR funcName)
{
    //g_pClient->OnGdiText(text, hdc, X, Y, funcName);
}



///////////////////////////////////////////////////////////////////////////////
// Our custom version of the TextOut Windows API.
// http://msdn.microsoft.com/en-us/library/ms534019.aspx
///////////////////////////////////////////////////////////////////////////////
BOOL WINAPI Mine_TextOut(HDC hdc,int X,int Y,LPCWSTR text,int textLen)
{
    BOOL rv = Real_TextOut(hdc, X, Y, text, textLen);
    //g_pClient->OnGdiText(text, hdc, X, Y, L"TextOut");
    return rv;
}



///////////////////////////////////////////////////////////////////////////////
// Our custom version of the ExtTextOut Windows API.
// http://msdn.microsoft.com/en-us/library/ms533949(VS.85).aspx
///////////////////////////////////////////////////////////////////////////////
BOOL WINAPI Mine_ExtTextOut(HDC hdc, int X, int Y, UINT options, RECT* lprc, LPCWSTR text, UINT cbCount, INT* lpSpacingValues)
{
    BOOL rv = Real_ExtTextOut(hdc, X, Y, options, lprc, text, cbCount, lpSpacingValues);
    //g_pClient->OnGdiText(text, hdc, X, Y, L"ExtTextOut");
    return TRUE;
}



///////////////////////////////////////////////////////////////////////////////
// Our custom version of the DrawText Windows API. Currently, we're only 
// paying attention to DrawText calls for the PokerStars client. FullTilt
// doesn't use DrawTExt.
//
// http://crawlmsdn.microsoft.com/en-us/library/ms533909(VS.85).aspx
///////////////////////////////////////////////////////////////////////////////
int WINAPI Mine_DrawText(HDC hdc, LPCWSTR text,  int nCount, LPRECT lpRect, UINT uOptions)
{
    int rv = Real_DrawText(hdc, text, nCount, lpRect, uOptions);
    //g_pClient->OnGdiText(text, hdc, lpRect->left, lpRect->top, L"DrawText");
    return rv;
}



///////////////////////////////////////////////////////////////////////////////
// Our custom version of the DrawTextEx Windows API.
// http://msdn.microsoft.com/en-us/library/ms534007(VS.85).aspx
///////////////////////////////////////////////////////////////////////////////
int WINAPI Mine_DrawTextEx(HDC hdc, LPWSTR text, int textLength, LPRECT lpRect, UINT uFormat, LPDRAWTEXTPARAMS lpParams)
{
    int rv = Real_DrawTextEx(hdc, text, textLength, lpRect, uFormat, lpParams);
    //g_pClient->OnGdiText(text, hdc, lpRect->left, lpRect->top, L"DrawTextEx");
    return rv;
}



///////////////////////////////////////////////////////////////////////////////
// Our custom version of the OpenFile Windows API (deprecated).
// http://msdn.microsoft.com/en-us/library/aa365430(VS.85).aspx
///////////////////////////////////////////////////////////////////////////////
HFILE WINAPI Mine_OpenFile(LPCSTR lpFileName, LPOFSTRUCT lpReOpenBuff, UINT uStyle )
{
    HFILE hFile = Real_OpenFile(lpFileName, lpReOpenBuff, uStyle);
    //
    //// There is no Unicode version of OpenFile, so we have to convert..
    //int len = strlen(lpFileName) + 1;
    //LPWSTR file = new WCHAR[len];
    //if (::MultiByteToWideChar(CP_ACP, 0, lpFileName, -1, file, len))
    //    g_pClient->OnCreateFile(file, hFile, 0);
    //delete [] file;
    return hFile;
}




///////////////////////////////////////////////////////////////////////////////
// Our custom version of the CreateFile Windows API.
// http://msdn.microsoft.com/en-us/library/aa363858.aspx
///////////////////////////////////////////////////////////////////////////////
HANDLE WINAPI Mine_CreateFile(LPCWSTR lpFileName,DWORD dwDesiredAccess,DWORD dwShareMode,LPSECURITY_ATTRIBUTES lpSecurityAttributes,DWORD dwCreationDisposition,DWORD dwFlagsAndAttributes,HANDLE hTemplateFile)
{
    HANDLE hFile = Real_CreateFile(lpFileName,dwDesiredAccess,dwShareMode,lpSecurityAttributes,dwCreationDisposition,dwFlagsAndAttributes,hTemplateFile);
    //g_pClient->OnCreateFile(lpFileName, hFile, dwCreationDisposition);
    return hFile;
}



///////////////////////////////////////////////////////////////////////////////
// Our custom version of the WriteFile Windows API.
// http://msdn.microsoft.com/en-us/library/aa365747(VS.85).aspx
///////////////////////////////////////////////////////////////////////////////
BOOL WINAPI Mine_WriteFile(HANDLE hFile,LPCVOID lpBuffer,DWORD nNumberOfBytesToWrite,LPDWORD lpNumberOfBytesWritten,LPOVERLAPPED lpOverlapped)
{
    BOOL bSuccess = Real_WriteFile( hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, lpOverlapped);
    //g_pClient->OnWriteFile(hFile, lpBuffer, nNumberOfBytesToWrite);
    return bSuccess;
}



///////////////////////////////////////////////////////////////////////////////
// Our custom version of the CreateProcess Windows API.
// http://msdn.microsoft.com/en-us/library/ms682425(VS.85).aspx
///////////////////////////////////////////////////////////////////////////////
BOOL WINAPI Mine_CreateProcess(LPCWSTR lpApplicationName,LPWSTR lpCommandLine,LPSECURITY_ATTRIBUTES lpProcessAttributes,LPSECURITY_ATTRIBUTES lpThreadAttributes,BOOL bInheritHandles,DWORD dwCreationFlags,LPVOID lpEnvironment,LPCWSTR lpCurrentDirectory,LPSTARTUPINFOW lpStartupInfo,LPPROCESS_INFORMATION lpProcessInformation)
{
    BOOL bSuccess = Real_CreateProcess( lpApplicationName, lpCommandLine, lpProcessAttributes, lpThreadAttributes, bInheritHandles, dwCreationFlags, lpEnvironment, lpCurrentDirectory, lpStartupInfo, lpProcessInformation);
    //g_pClient->OnCreateProcess(lpApplicationName, lpProcessInformation->hProcess);
    return bSuccess;
}



///////////////////////////////////////////////////////////////////////////////
// Our custom version of the OpenProcess Windows API.
// http://msdn.microsoft.com/en-us/library/ms684320(VS.85).aspx
///////////////////////////////////////////////////////////////////////////////
HANDLE WINAPI Mine_OpenProcess(DWORD dwDesiredAccess,BOOL bInheritHandle,DWORD dwProcessId)
{
    HANDLE hProcess = Real_OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId);
    //g_pClient->OnOpenProcess(hProcess);
    return hProcess;
}



///////////////////////////////////////////////////////////////////////////////
// Install the Detours for whatever APIs we're interested in here. Certain
// functions (such as text-painting) we'll only want to pay attention to for
// specific poker clients. Other functions, such as File I/O routines, we'll
// want to detour for ALL poker clients.
///////////////////////////////////////////////////////////////////////////////
void AttachDetours()
{
    /*switch(g_pClient->GetVenueID())
    {
    case Venue_Stars:
        DetourAttach(&(PVOID&)Real_DrawText, Mine_DrawText); break;
    case Venue_FullTilt:
        //DetourAttach(&(PVOID&)Real_DrawText, Mine_DrawText);
        //DetourAttach(&(PVOID&)Real_TextOut, Mine_TextOut);
        DetourAttach(&(PVOID&)Real_ExtTextOut, Mine_ExtTextOut);
        //DetourAttach(&(PVOID&)Real_DrawTextEx, Mine_DrawTextEx);
         break;
    };*/

    //DetourAttach(&(PVOID&)Real_OpenFile, Mine_OpenFile); deprecated
    //DetourAttach(&(PVOID&)Real_CreateFile, Mine_CreateFile);
    //DetourAttach(&(PVOID&)Real_WriteFile, Mine_WriteFile);
    //DetourAttach(&(PVOID&)Real_CreateProcess, Mine_CreateProcess);
    //DetourAttach(&(PVOID&)Real_OpenProcess, Mine_OpenProcess);
}




///////////////////////////////////////////////////////////////////////////////
// Un-install the Detours for whatever APIs we detoured above.
///////////////////////////////////////////////////////////////////////////////
void DetachDetours()
{
/*    switch(g_pClient->GetVenueID())
    {
    case Venue_Stars:
        DetourDetach(&(PVOID&)Real_DrawText, Mine_DrawText); break;
    case Venue_FullTilt:
        //DetourDetach(&(PVOID&)Real_DrawText, Mine_DrawText);
        //DetourDetach(&(PVOID&)Real_TextOut, Mine_TextOut);
        DetourDetach(&(PVOID&)Real_ExtTextOut, Mine_ExtTextOut);
        //DetourDetach(&(PVOID&)Real_DrawTextEx, Mine_DrawTextEx);
         break;
    };
*/

    //DetourDetach(&(PVOID&)Real_OpenFile, Mine_OpenFile);
    //DetourDetach(&(PVOID&)Real_CreateFile, Mine_CreateFile);
    //DetourDetach(&(PVOID&)Real_WriteFile, Mine_WriteFile);
    //DetourDetach(&(PVOID&)Real_CreateProcess, Mine_CreateProcess);
    //DetourDetach(&(PVOID&)Real_OpenProcess, Mine_OpenProcess);
}



void CatchWindows()
{
	

}